package com.leetcode.alibaba;

/**
 * 比如"Hello World"变形后就变成了"wORLD hELLO"。
 */
public class 字符串变形 {

	public static void main(String[] args) {
		String this_is_a_sample = trans("This is a sample", 16);
		System.out.println(this_is_a_sample);
	}

	public static String trans(String s, int n) {
		// 这里需要加 -1，保证最后如果有空格也能保留空格
		String[] str = s.split(" ", -1);
		StringBuilder sb = new StringBuilder();
		for (int i = str.length - 1; i >= 0; i--) {
			sb.append(reverse(str[i]));
			if (i == 0) {
				break;
			}
			sb.append(" ");
		}
		return sb.toString();
	}

	// 大小字母转换
	private static String reverse(String s) {
		StringBuilder builder = new StringBuilder();
		for (char c : s.toCharArray()) {
			if (Character.isUpperCase(c)) {
				builder.append(Character.toLowerCase(c));
			} else {
				builder.append(Character.toUpperCase(c));
			}
		}
		return builder.toString();
	}
}
